package com.smcdd.cdb.dbo.cdb_dbo.ui.widget;

import android.app.Dialog;
import android.content.Context;
import android.net.wifi.ScanResult;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.MediaController;
import android.widget.TextView;

import com.smcdd.cdb.dbo.cdb_dbo.R;
import com.smcdd.cdb.dbo.cdb_dbo.ui.adapter.WifiDialogAdapter;

import java.util.List;

/**
 * 作者：Administrator on 2017/6/27 21:42
 * 邮箱：zhanghuaiha@gmail.com
 */

public class ListDialog extends Dialog {

    private TextView titleTv;//消息标题文本
    private TextView messageTv;//消息提示文本
    private String titleStr;//从外界设置的title文本
    private String messageStr;//从外界设置的消息文本
    private RecyclerView mRecycler;
    private WifiDialogAdapter wifiDialogAdapter;
    private Context mContext;
    List<ScanResult> list;
    private OnItemClickListenter onItemClickListenter;


    public ListDialog(Context context,List<ScanResult> list) {
        super(context, R.style.MyDialog);
        mContext = context;
        this.list = list;
    }

    public interface OnItemClickListenter {
        void itemClickListenter(ScanResult scan, int position);
    }

    public void setItemClickListenter(OnItemClickListenter itemClickListenter) {
        onItemClickListenter = itemClickListenter;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        setContentView(R.layout.dialog_list);
//        //按空白处不可以取消动画
//        setCanceledOnTouchOutside(false);
//        wifiDialogAdapter = new WifiDialogAdapter();
//        //初始化界面控件
//        initView();
//        //初始化界面数据
//        initData();
    }

    /**
     * 初始化界面控件的显示数据
     */
    public void initData() {
        //如果用户自定了title和message
        if (titleStr != null) {
            titleTv.setText(titleStr);
        }
        if (list != null) {
            wifiDialogAdapter.addData(list);
        }

        wifiDialogAdapter.setItemClickListenter((scan, position) -> {
            onItemClickListenter.itemClickListenter(scan,position);
        });
    }

    /**
     * 初始化界面控件
     */
    private void initView() {
        titleTv = (TextView) findViewById(R.id.title);
        mRecycler = (RecyclerView) findViewById(R.id.dialog_recycler);
        mRecycler.setLayoutManager(new LinearLayoutManager(mContext));
        mRecycler.setAdapter(wifiDialogAdapter);
    }

    /**
     * 从外界Activity为Dialog设置标题
     *
     * @param title
     */
    public void setTitle(String title) {
        titleStr = title;
    }

    /**
     * 从外界Activity为Dialog设置dialog的message
     *
     * @param
     */
    public void setDate(List<ScanResult> list) {
        this.list = list;
        wifiDialogAdapter.addData(list);
    }

    @Override
    public void show() {
        super.show();
        setContentView(R.layout.dialog_list);
        //按空白处不可以取消动画
        setCanceledOnTouchOutside(false);
        wifiDialogAdapter = new WifiDialogAdapter();
        //初始化界面控件
        initView();
        //初始化界面数据
        initData();
    }
}
